<?php namespace Core\Myapp\Helper\Admin\Form\Group;

class Group
{
    protected $_id;
    protected $_title;
    protected $_fields = array();
    protected $_sortOrder = 0;

    public function __construct($id, $title, $sortOrder)
    {
        $this->_id = $id;
        $this->_title = $title;
        $this->_sortOrder = $sortOrder;
    }

    public function getSortOrder()
    {
        return $this->_sortOrder;
    }

    public function addField($data)
    {
        $type = ucwords($data['type']);
        $namespace = 'Core\Myapp\Helper\Admin\Form\Group\Element\\' . $type;
        $this->_fields[$data['name']] = new $namespace($data);
        return $this;
    }

    public function getField($name)
    {
        return $this->_fields[$name];
    }

    public function render()
    {
        ob_start();
        echo '<div id="'.$this->_id.'" class="form-group">';
        echo '<h3>' . $this->_title . '</h3>';
        foreach($this->_fields as $field) {
            echo $field->render();
        }
        echo '</div>';
        $html = ob_get_clean();
        return $html;
    }
}